//
//  HWHomeInfoDetailView.swift
//  HWanGame
//
//  Created by Apple on 2024/8/8.
//  Copyright © 2024 94Wan. All rights reserved.
//

import UIKit
import Stevia

class HWHomeInfoDetailGameView: UIView, UICollectionViewDelegate, UICollectionViewDataSource {
    
    private let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
    var dataSource: [HWHomeInfoDetailGameModel]?{
        didSet{
            collectionView.reloadData()
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = .clear
        
        subviews{
            collectionView
        }
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 12*widthScale
        layout.minimumInteritemSpacing = 12*widthScale
        layout.scrollDirection = .horizontal
        layout.sectionInset = UIEdgeInsets(top: 0, left: 12*widthScale, bottom: 0, right: 0)
        layout.itemSize = CGSize(width: 300*widthScale, height: 60*widthScale)
        
        collectionView.collectionViewLayout = layout
        collectionView.backgroundColor = .clear
        collectionView.register(HWWuKongNormalGameCell.self, forCellWithReuseIdentifier: HWWuKongNormalGameCell.description())
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.delegate = self
        collectionView.dataSource = self
        
        collectionView.fillContainer()
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataSource?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: HWWuKongNormalGameCell.description(), for: indexPath) as? HWWuKongNormalGameCell else { return UICollectionViewCell() }
        cell.model = dataSource?[indexPath.row]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class HWHomeInfoCommitView: UIView, UITableViewDelegate, UITableViewDataSource {
    
    private let titleLabel = c_make_ex(UILabel.self)
        .text("资讯评论").font(14.font_semiBold).color(.black).end()
    private let moreButton = c_make_ex(UIButton.self)
        .text("查看全部 >").font(15.font_semiBold).color("#47494D").end()
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = .clear
        tableView.showsVerticalScrollIndicator = false
        tableView.register(HWCommitCell.self)
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.separatorStyle = .none
        tableView.estimatedRowHeight = 150
        tableView.bounces = false
        return tableView
    }()
    var commitDetailVC: ((_ commitId: String) -> Void)?
    var toAllCommitListVC: (() -> Void)?
    var likeCommit: ((_ commitID: String) -> Void)?
    
    var models: [HWInfoCommentListModel]? {
        didSet{
            if let array = models {
                let md = array[0]
                let height = getTextHeigh(textStr: md.content, font: 15.font_medium!, width: screenWidth-104*widthScale) + 120.0
                tableView.heightConstraint?.constant = height
            }
            tableView.reloadData()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = .clear
        
        subviews{
            titleLabel
            tableView
            moreButton
        }
        
        titleLabel.leading(12*widthScale).top(22*widthScale)
        tableView.leading(0).trailing(0).top(55*widthScale)
        moreButton.leading(0).trailing(0).bottom(0).height(35*widthScale)
        moreButton.Top == tableView.Bottom
        
        moreButton.addTarget(self, action: #selector(clickMoreButton), for: .touchUpInside)
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        //跳转到详情
        commitDetailVC?(models?[0].id ?? "")
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

//        let commentCell = HWGameDetailCommentCell.cellWithTableView(tableView: tableView)
//        commentCell.commitModel = models?[indexPath.row]
//        return commentCell

        let cell = tableView.dequeueReusableCell(withIdentifier: "HWCommitCell", for: indexPath) as! HWCommitCell
        cell.commitModel = models?[0]
        cell.likeCommitBlock = { id in
            self.likeCommit?(id)
        }
        return cell
    }
    
//    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
//        if let md = models?[indexPath.row] {
//            let height = getTextHeigh(textStr: md.content, font: 15.font_medium!, width: screenWidth-104*widthScale) + 98.0
//            return height
//        }else{
//            return 0.0
//        }
//        
//    }
    
    @objc private func clickMoreButton(){
        
        toAllCommitListVC?()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class HWHomeInfoDetailBottomView: UIView {

    let likeButton = UIIconButton(style: .top(itemSpace: 2))
    let collectButton = UIIconButton(style: .top(itemSpace: 2))
    private let commitButton = UIIconButton(style: .top(itemSpace: 2))
    private let textFiedlButton = c_make_ex(UIButton.self)
        .bgColor("#000000".toColor.alpha(0.05)).corner(16*widthScale)
        .color("#7B7D80").font(11.font_medium).text("写点什么...").end()
    
    var showCommitView: (() -> Void)?
    var likeButtonBlock: (() -> Void)?
    var collectButtonBlock: (() -> Void)?
    
    var model:HWHomeInfoDetailDataModel?{
        didSet{
            guard let md = model else { return }
            likeButton.isSelected = md.is_zan == 1
            collectButton.isSelected = md.is_shoucang == 1
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = "#FDFDFD".toColor
        
        subviews{
            likeButton
            collectButton
            commitButton
            textFiedlButton
        }
        
        likeButton.style { v in
            v.iconSize = CGSize(width: 24*widthScale, height: 24*widthScale)
            v.setImage("Detail_like_nor".toImage, for: .normal)
            v.setImage("Detail_like_sel".toImage, for: .selected)
            v.setTitle("点赞", for: .normal)
            v.setTitleColor("#2E3033".toColor, for: .normal)
            v.setTitleColor("#FF7600".toColor, for: .selected)
            v.titleLabel?.font = 10.font_medium
            v.addTarget(self, action: #selector(clickLikeButton), for: .touchUpInside)
        }
        collectButton.style { v in
            v.iconSize = CGSize(width: 24*widthScale, height: 24*widthScale)
            v.setImage("Detail_collect_nor".toImage, for: .normal)
            v.setImage("Detail_collect_sel".toImage, for: .selected)
            v.setTitle("收藏", for: .normal)
            v.setTitleColor("#2E3033".toColor, for: .normal)
            v.setTitleColor("#FF7600".toColor, for: .selected)
            v.titleLabel?.font = 10.font_medium
            v.addTarget(self, action: #selector(cliclCollectButton), for: .touchUpInside)
        }
        commitButton.style { v in
            v.iconSize = CGSize(width: 24*widthScale, height: 24*widthScale)
            v.setImage("Detail_commit_nor".toImage, for: .normal)
            v.setImage("Detail_commit_sel".toImage, for: .selected)
            v.setTitle("评论", for: .normal)
            v.setTitleColor("#2E3033".toColor, for: .normal)
            v.setTitleColor("#FF7600".toColor, for: .selected)
            v.titleLabel?.font = 10.font_medium
            v.addTarget(self, action: #selector(startCommit), for: .touchUpInside)
        }
        textFiedlButton.addTarget(self, action: #selector(startCommit), for: .touchUpInside)
        
        likeButton.height(40*widthScale).width(44*widthScale).trailing(12*widthScale).top(11*widthScale)
        collectButton.height(40*widthScale).width(44*widthScale).trailing(56*widthScale)
        collectButton.CenterY == likeButton.CenterY
        commitButton.height(40*widthScale).width(44*widthScale).trailing(100*widthScale)
        commitButton.CenterY == collectButton.CenterY
        textFiedlButton.leading(12*widthScale).height(32*widthScale).width(160*widthScale).top(11*widthScale)
        
    }
    
    @objc private func clickLikeButton(){
        likeButtonBlock?()
    }
    
    @objc private func cliclCollectButton(){
        collectButtonBlock?()
    }
    
    @objc private func startCommit(){
        showCommitView?()
    }

    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class HWHomeInfoDetailCommitView: UIView, UITextViewDelegate {

    private let sendButton = c_make_ex(UIButton.self)
        .image("Detail_send_nor").image("Detail_send_sel",state: .selected).end()
    lazy var commitView: Custom_TextView = {
        let view = Custom_TextView()
        view.backgroundColor = "#000000".toColor.alpha(0.05)
        view.textContainerInset = UIEdgeInsets(top: 10, left: 12, bottom: 10, right: 15)
        view.tintColor = .black
        view.textColor = .black
        view.placeHolderColor = "#7B7D80".toColor
        view.placeHolder = "写点什么..."
        view.placeholderFont = 11.font_medium
        view.returnKeyType = .done
        view.font = 11.font_medium
        view.layer.cornerRadius = 16
        view.layer.masksToBounds = true
        return view
    }()
    var sendTextBlock: ((_ text: String) -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = .white.alpha(0.8)
        
        subviews{
            commitView
            sendButton
        }

        commitView.delegate = self
        commitView.leading(12*widthScale).height(32*widthScale).trailing(65*widthScale).top(12*widthScale)
        sendButton.trailing(12*widthScale).height(32*widthScale).width(45*widthScale).top(8*widthScale)
        
        sendButton.addTarget(self, action: #selector(sendMessage), for: .touchUpInside)
    }
    
    @objc private func sendMessage(){
        guard let text = commitView.text, text.count > 0 else { return }
        sendTextBlock?(text)
    }

    // UITextViewDelegate 方法
    func textViewDidChange(_ textView: UITextView) {
        // 获取当前字符数
        let characterCount = textView.text.count
        sendButton.isSelected = characterCount > 0
        // 可在此处添加其他逻辑，例如限制字符长度
        if characterCount > 200 {
            textView.text = String(textView.text.prefix(200))
        }
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
